package controllers

import (
	"git.oschina.net/goxiang2/web/admin/models"
	"github.com/astaxie/beego"
	"time"
)

type LogController struct {
	beego.Controller
}

// 提供给客户端的接口
func (c *LogController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/login")
		return
	}

	user_permission := models.GetPermission(userinfo.(*LoginInfo).Username)
	if (user_permission & (1 << uint(models.ModelUser))) == 0 {
		c.Ctx.Redirect(302, "/index")
		return
	}
	data := getIndex(user_permission)
	for k, v := range data {
		c.Data[k] = v
	}

	c.Data["title"] = beego.AppConfig.String("apptitle")
	c.Data["username"] = userinfo.(*LoginInfo).Username
	c.Data["token"] = models.GetToken()

	username := c.GetString("username")
	ts1 := c.GetString("begin_ts")
	ts2 := c.GetString("end_ts")
	count, _ := c.GetInt("count")
	operate := c.GetString("operate")

	if count == 0 {
		count = 50
	}

	begin_ts := StrToTime(ts1)
	end_ts := StrToTime(ts2)

	c.Data["logs"] = models.QueryLog(username, begin_ts, end_ts, operate, count)

	day := time.Now().Format("2006-01-02")
	if len(ts1) != 19 {
		ts1 = day + " 00:00:00"
	}
	if len(ts2) != 19 {
		ts2 = day + " 23:59:59"
	}
	c.Data["begin"] = ts1
	c.Data["end"] = ts2
	c.TplName = "log.ace"
}
